package org.xxforest.game.core.scenes.scene2d.ui;

import org.xxforest.game.core.scenes.scene2d.XXNode;
import org.xxforest.game.core.scenes.scene2d.graphics.XXBitmapFont;
import org.xxforest.game.core.scenes.scene2d.graphics.XXBitmapFontCache;
import org.xxforest.game.core.scenes.scene2d.graphics.XXBitmapFont.XXTextBounds;
import org.xxforest.game.core.utils.XXPoint;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class XXLable extends XXNode {
	
	private XXBitmapFontCache cache ;
	private XXBitmapFont bitmapFont ;
	private String label_contents ;
	
	public XXLable(XXBitmapFont font , String label_contents){
		this.bitmapFont = font ;
		this.cache = new XXBitmapFontCache(font);
		setText(label_contents);
	}
	
	public void setText(String label_contents) {
		cache.clear() ;
		this.label_contents = label_contents ;
		XXTextBounds bounds = cache.addText(label_contents, getX(), getY(), 0, label_contents.length());
		setWidth(bounds.width );
		setHeight(bounds.height) ;
		this.setAnchorPoint(XXPoint.create(this.getWidth() / 2, this.getHeight() / 2));
	}

	public static XXLable create(XXBitmapFont bitmapFont , String label_contents){
		return new XXLable(bitmapFont, label_contents);
	}
	
	public static XXLable create(String label_contents){
		return new XXLable(new XXBitmapFont(), label_contents);
	}

	@Override
	public void draw(SpriteBatch batch , float delta) {
		Color color = getColor();
		
		float parentAlpha = getDrawAlpha() ;
		batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);

		float x = getDrawX();
		float y = getDrawY();
		bitmapFont.draw(batch, label_contents, x , y );
	}

	@Override
	public void onCreate() {
		
	}
}
